package com.flink.source;

import org.apache.commons.lang3.RandomUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSONObject;

public class WebDataProducer {

    public static String webDataProducer() throws Exception{
        //构建web对象，在ip为10.117后面加两个随机数
        int randomInt1 = RandomUtils.nextInt(1,255);
        int randomInt2 = RandomUtils.nextInt(1,999);
        int randomInt3 = RandomUtils.nextInt(1,99999);
        List<String> areas = Arrays.asList("深圳", "广州", "上海", "北京", "武汉", "合肥", "杭州", "南京");
        List<String> webs = Arrays.asList("www.taobao.com","www.baidu.com","www.jd.com","www.weibo.com","www.qq.com","www.weixin.com","www.360.com","www.lzl.com","www.xiaomi.com");
        List<String> operates = Arrays.asList("register","view","login","buy","click","comment","jump","care","collect");

        Data web = new Data();  //实例化一个web对象，并向对象中放入数据
        web.setUuid("uid_" + randomInt3);
        web.setIp("10.110." + randomInt1 +"." + randomInt2);
        web.setArea(getRandomElement(areas));
        web.setWeb(getRandomElement(webs));
        web.setOperate(getRandomElement(operates));
        web.setCreateDate(new Date());

        // 转换成JSON格式
        String webJson = JSONObject.toJSONString(web);
        System.out.println(webJson); //打印出来看看效果
        return webJson;
    }
    //构建一个从列表里面任意筛选一个元素的函数方法
    public static <T> T getRandomElement(List<T> list) {
        return list.get(RandomUtils.nextInt(0,8));
    }

    public static void main(String[] args) {
        while (true) {
            try {
                // 每三秒写一条数据
                Thread.sleep(3000);
                webDataProducer();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
